package edu.gatech.cs2340.mule.view;

import java.awt.Color;
import java.awt.event.ActionListener;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.ButtonGroup;
import javax.swing.JLabel;
import javax.swing.JRadioButton;
import javax.swing.JComboBox;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;

import edu.gatech.cs2340.mule.presenter.MuleGamePresenter;

/**
 * This class is the initial configuration screen that appears.
 * The user can set the difficulty of the game, the map style they
 * desire, and how many players will play. After clicking OK, player
 * configuration begins.
 * 
 * @author JR O'Donnell
 * 
 */
public class GameSetUpFrame extends JFrame {
	
	private JPanel contentPane;
	private JButton okButton;
	private JRadioButton beginnerRadioButton;
	private JRadioButton standardRadioButton;
	private JRadioButton classicRadioButton;
	private JRadioButton randomRadioButton;
	private JComboBox numberPlayersComboBox = new JComboBox();

	/**
	 * Create the frame.
	 */
	public GameSetUpFrame() {
		setVisible(true);
		setTitle("M.U.L.E.");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 451, 300);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));

		contentPane.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		setContentPane(contentPane);
		contentPane.setLayout(null);
		
		JPanel panel = new JPanel();
		panel.setBounds(6, 6, 438, 41);
		panel.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		contentPane.add(panel);
		
		JLabel lblDifficulty = new JLabel("<html><font size=+1>Difficulty:");
		lblDifficulty.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		panel.add(lblDifficulty);
		
		beginnerRadioButton = new JRadioButton("<html><font size=+1>Beginner");
		beginnerRadioButton.setSelected(true);
		beginnerRadioButton.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		beginnerRadioButton.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		panel.add(beginnerRadioButton);
		
		standardRadioButton = new JRadioButton("<html><font size=+1>Standard");
		standardRadioButton.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		standardRadioButton.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		panel.add(standardRadioButton);
		
		ButtonGroup diffGroup = new ButtonGroup();
		diffGroup.add(beginnerRadioButton);
		diffGroup.add(standardRadioButton);
		
		JPanel panel_1 = new JPanel();
		panel_1.setBounds(6, 49, 438, 51);
		panel_1.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		contentPane.add(panel_1);
		
		JLabel mapTypeLabel = new JLabel("<html><font size=+1>Map Type:");
		mapTypeLabel.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		panel_1.add(mapTypeLabel);
		
		classicRadioButton = new JRadioButton("<html><font size=+1>Classic");
		classicRadioButton.setSelected(true);
		classicRadioButton.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		classicRadioButton.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		panel_1.add(classicRadioButton);
		
		randomRadioButton = new JRadioButton("<html><font size=+1>Random");
		randomRadioButton.setEnabled(false); //random maps available in further patch
		randomRadioButton.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		randomRadioButton.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		panel_1.add(randomRadioButton);
		
		ButtonGroup typeGroup = new ButtonGroup();
		typeGroup.add(classicRadioButton);
		typeGroup.add(randomRadioButton);
		
		JPanel panel_2 = new JPanel();
		panel_2.setBounds(6, 102, 438, 51);
		panel_2.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		contentPane.add(panel_2);
		
		JLabel lblNumberOfPlayers = new JLabel("<html><font size=+1>Number of Players");
		lblNumberOfPlayers.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		panel_2.add(lblNumberOfPlayers);
		
		numberPlayersComboBox = new JComboBox();
		numberPlayersComboBox.setModel(new DefaultComboBoxModel(new String[] 
				{"<html><font size=+1>2", "<html><font size=+1>3",
				"<html><font size=+1>4"}));
		numberPlayersComboBox.setSelectedIndex(0);
		numberPlayersComboBox.setForeground(MuleGamePresenter.FOREGROUND_COLOR);
		numberPlayersComboBox.setBackground(MuleGamePresenter.BACKGROUND_COLOR);
		panel_2.add(numberPlayersComboBox);
		
		okButton = new JButton("<html><font size=+1>OK");
		okButton.setBounds(166, 160, 117, 29);
		okButton.setBackground(MuleGamePresenter.FOREGROUND_COLOR);
		okButton.setForeground(MuleGamePresenter.BACKGROUND_COLOR);
		contentPane.add(okButton);
		
		//explicitly resize window to force repaint of all components
		setBounds(100, 100, 450, 300);
	}
	
	/**
	 * 
	 * @param a The action listener to add to the OK button
	 */
	public void registerOK(ActionListener a) {
		okButton.addActionListener(a);
	}
	
	/**
	 * 
	 * @return The difficulty level the user selected
	 */
	public String getDifficulty() {
		if(beginnerRadioButton.isSelected()) {
			return "Beginner";
		}
		else {
			return "Standard";
		}
	}
	
	/**
	 * 
	 * @return The map type the user selected
	 */
	public String getMapType() {
		if(classicRadioButton.isSelected()) {
			return "Classic";
		}
		else {
			return "Random";
		}
	}
	
	/**
	 * 
	 * @return The number of players the user selected
	 */
	public int getPlayerCount() {
		return numberPlayersComboBox.getSelectedIndex() + 2;
	}
}
